home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / zoo21src.lha / zoo / zoo.c < prev    next >
C/C++ Source or Header  |  1992-11-20  |  16KB  |  534 lines

  1. #ifndef LINT
  2. /* derived from: zoo.c 2.24 88/01/29 00:55:09 */
  3. static char sccsid[]="$Id: zoo.c,v 1.5 1991/07/24 23:47:04 bjsjr Rel $";
  4. #endif /* LINT */
  5.  
  6. #if 0
  7. #define TRACEI(item)    printf("line %d: %s= %d\n", __LINE__, #item, item)
  8. #define TRACES(item)    printf("line %d: %s= [%s]\n", __LINE__, #item, item)
  9. #endif
  10.  
  11. extern char version[];
  12.  
  13. /*
  14. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  15. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  16. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  17. */
  18. #include "options.h"
  19. #include "zoo.h"
  20. #include "zooio.h"
  21. #include "various.h"
  22.  
  23. #include "zoofns.h"
  24.  
  25. #include "errors.i"
  26. #include "zoomem.h"
  27.  
  28. static void ms_help PARMS((char *));
  29. static void wait_return PARMS((void));
  30.  
  31. #ifdef TRACE_IO
  32. int verbose = 0;
  33. #endif
  34.  
  35. int instr PARMS ((char *, char *));
  36.  
  37. char *out_buf_adr;      /* points to memory allocated for output buffer(s) */
  38. char *in_buf_adr;       /* points to memory allocated for input buffer */
  39.  
  40. /* static declarations */
  41. int quiet = 0;             /* whether to be quiet */
  42. int next_arg = FIRST_ARG; /* filenames start at this position */
  43. int arg_count;          /* count of arguments supplied to program */
  44. char **arg_vector;      /* vector of arguments supplied to program */
  45.  
  46. #ifndef GLOB
  47. int main(argc,argv)
  48. #else
  49. int zoomain(argc,argv)
  50. #endif
  51. register int argc;
  52. register char **argv;
  53. {
  54.    char *zooname;          /* synonym for argv[2] -- to make life easier */
  55. #ifndef OOZ
  56.    static char incorrect_args[] = "Incorrect number of arguments.\n";
  57.    int filecount;          /* how many filespecs supplied */
  58. #endif /* OOZ */
  59.  
  60. #ifdef OOZ
  61. #else
  62. /* else not OOZ */
  63.       static char usage[] = "Usage: zoo {acDeglLPTuUvx}[aAcCdEfInmMNoOpPqu1:/.@n] archive file\n(\"zoo h\" for help, \"zoo H\" for extended help)\n";
  64.       static char nov_usage[] =
  65.           "\nNovice usage:  zoo -cmd archive[.zoo] file...  where -cmd is one of these:\n";
  66.       char *option;
  67. #ifndef TOS
  68.       static char nov_cmds[] =
  69.          /* ADD=0EXT=5    MOV=14TES=20PRI=26 DEL=33  LIS=41UPD=47  FRE=55   COMMENT=64 */
  70.            "-add -extract -move -test -print -delete -list -update -freshen -comment\n";
  71. #else
  72.       static char nov_cmds[] = 
  73. "-add -extract -move -test -print -delete -list -update -freshen -comment\n-backup -restore\n";
  74. #endif  /* TOS */
  75.  
  76. #ifdef NOENUM
  77. #define NONE   -1
  78. #define ADD    0
  79. #define EXTRACT 5
  80. #define MOVE   14
  81. #define TEST   20
  82. #define PRINT  26
  83. #define DELETE 33
  84. #define LIST   41
  85. #define UPDATE 47
  86. #define FRESHEN   55
  87. #define COMMENT   64
  88.  
  89. int cmd = NONE;
  90.  
  91. #else
  92.    enum choice {
  93.       NONE = -1, ADD = 0, EXTRACT = 5, MOVE = 14, TEST = 20, PRINT = 26,
  94.       DELETE = 33, LIST = 41, UPDATE = 47, FRESHEN = 55, COMMENT = 64
  95.    };
  96.    enum choice cmd = NONE;          /* assume no Novice command */
  97. #endif
  98.  
  99. #endif /* end of not OOZ */
  100.  
  101. #ifdef SPECINIT
  102.     void spec_init PARMS ((void));
  103.     spec_init();                            /* system-specific startup code */
  104. #endif
  105.  
  106.     /* make sure T_UINT16 is an unsigned 16-bit type, exactly.  This
  107.         code is included only if T_UINT16 was defined by default at the
  108.         end of options.h. */
  109. #ifdef CHECK_TUINT
  110.     {
  111.         T_UINT16 i;
  112.         int status = 0;
  113.         i = ((unsigned) 1) << 15;
  114.         if (i < 0)
  115.             status = 1;
  116.         if (i != ((unsigned) 1) << 15)
  117.             status = 1;
  118.         i *= 2;
  119.         if (i != 0)
  120.             status = 1;
  121.         if (status != 0)
  122.             prterror('w', "Configuration problem: T_UINT16 is not 16 bits\n");
  123.     }
  124. #endif
  125.  
  126.    arg_count = argc;
  127.    arg_vector = argv;
  128.    zooname = argv[FIRST_ARG-1];     /* points to name or archive */
  129.  
  130. #ifdef OOZ
  131.    if (argc < 2) {
  132.       putstr (usage1);
  133.       putstr (usage2);
  134.       zooexit (1);
  135.    }
  136. #else
  137. /* else not OOZ */
  138.    if (argc < 2)
  139.       goto show_usage;
  140.    filecount = argc - 3;
  141.    option = str_dup(argv[1]);
  142.  
  143. #ifdef TRACE_IO
  144.    if (*option == ':') {         /* for debugging output */
  145.       verbose++;
  146.       option++;                  /* hide the : from other functions */
  147.    }
  148. #endif
  149.  
  150. #ifdef WAIT_PROMPT
  151.    if (*option == 'w') {
  152.         option++;                        /* hide w from other functions */
  153.         wait_return();
  154.     }
  155. #endif /* WAIT_PROMPT */
  156.  
  157.    if (*option == 'H') ms_help(option);
  158.    if (*option == 'h' || *option == 'H')
  159.       goto bigusage;
  160.     if (strchr("-acDegflLPTuUvVx", *option) == NULL)
  161.         goto give_list;
  162.  
  163.    if (*option == '-') {
  164. #ifdef NOENUM
  165.       cmd = instr (nov_cmds, str_lwr(option));
  166. #else
  167.       cmd = (enum choice) instr (nov_cmds, str_lwr(option));
  168. #endif
  169.  
  170.       if (strlen(option) < 2 || cmd == NONE)
  171.          goto show_usage;
  172.  
  173.       if (  ((cmd == ADD || cmd == MOVE || cmd == FRESHEN ||
  174.                   cmd == UPDATE || cmd == DELETE) && argc < 4) ||
  175.             ((cmd == EXTRACT || cmd == TEST || cmd == LIST ||
  176.                      cmd == PRINT || cmd == COMMENT) && argc < 3)) {
  177.          fprintf (stderr, incorrect_args);
  178.          goto show_usage;
  179.       }
  180.    } else {
  181.         char *wheresI;        /* will be null if I option not supplied */
  182.         if    (
  183.                 (
  184.                     strchr("au",*option) &&
  185.                     (
  186.                         (((wheresI = strchr(option,'I')) != 0) &&
  187.                             argc != 3) ||
  188.                         wheresI==NULL && argc < 4
  189.                     )
  190.                 ) ||
  191.                  strchr("DU",*option) && argc < 4 ||
  192.              strchr("cexlvVL",*option) && argc < 3 ||
  193.              strchr("TP",*option)   && argc != 3 ||
  194.                  (*option == 'f' && argc != 2) ||
  195.                  (*option == 'g' &&
  196.                     (strchr(option,'A') == NULL && argc < 4 ||
  197.                      strchr(option,'A') != NULL && argc != 3
  198.                     )
  199.                  )
  200.             ) {
  201.          fprintf (stderr, incorrect_args);
  202.          goto show_usage;
  203.       }
  204.    }
  205. #endif /* end of not OOZ */
  206.  
  207. #ifndef OOZ
  208.    /* if not doing a list and no extension in archive name, add default
  209.    extension */
  210.    if (*option != 'f' && cmd != LIST && strchr("lvVL", *option) == NULL &&
  211.          strchr(nameptr (zooname), EXT_CH) == NULL)
  212.       zooname = newcat (zooname, EXT_DFLT);
  213. #endif
  214.  
  215. /*
  216. Here we allocate a large block of memory for the duration of the program.
  217. lzc() and lzd() will use half of it each.  Routine getfile() will use all
  218. of it.  Routine decode() will use the first 8192 bytes of it.  Routine
  219. encode() will use all of it. */
  220.  
  221. /*                          fudge/2           fudge/2
  222. **             [______________||________________|]
  223. **               output buffer    input buffer
  224. */
  225.    out_buf_adr = ealloc (MEM_BLOCK_SIZE);
  226.    in_buf_adr = out_buf_adr + OUT_BUF_SIZE + (FUDGE/2);
  227.  
  228. #ifdef OOZ
  229. zooext(zooname, "\0");     /* just extract -- no fancy stuff   */
  230. zooexit (0);                  /* and exit normally                */
  231. #else
  232. /* else not OOZ -- parse command line and invoke a routine */
  233.    if (cmd != NONE) {
  234.       switch (cmd) {
  235.  
  236.          case ADD:      zooadd (zooname, filecount, &argv[3], "ahP:"); break;
  237.          case FRESHEN:  zooadd (zooname, filecount, &argv[3], "auhP:"); break;
  238.          case UPDATE:   zooadd (zooname, filecount, &argv[3], "auhnP:"); break;
  239.          case MOVE:     zooadd (zooname, filecount, &argv[3], "aMhP:"); break;
  240.          case EXTRACT:  zooext (zooname, "x"); break;
  241.          case TEST:     zooext (zooname, "xNd"); break;
  242.          case PRINT:    zooext (zooname, "xp"); break;
  243.  
  244.          case DELETE:   zoodel (zooname, "DP",1); break;
  245.          case LIST:     zoolist (&argv[2], "VC", argc-2); break;
  246.          case COMMENT:  comment (zooname, "c"); break;
  247.          default: goto show_usage;
  248.       }
  249.    } else
  250.       switch (*option) {
  251.  
  252.          case 'a':
  253.          case 'u':
  254.          case 'T':   
  255.             zooadd (zooname, filecount, &argv[3], option); break;
  256. #ifdef FILTER
  257.             case 'f':
  258.                 zoofilt (option);  break;
  259. #endif /* FILTER */
  260.          case 'D':
  261.             zoodel (zooname, option, 1); break;
  262.          case 'U':
  263.             zoodel (zooname, option, 0); break;
  264.             case 'g':
  265.                 zoodel (zooname, option, 2); break;
  266.          case 'v':
  267.             case 'V':
  268.          case 'l': 
  269.             zoolist(&argv[2], option, 1); break;
  270.          case 'L': 
  271.             zoolist(&argv[2], option, argc-2); break;
  272.          case 'e':
  273.          case 'x': 
  274.             zooext(zooname, option); break;
  275.          case 'P':
  276.             zoopack (zooname, option); break;
  277.          case 'c':
  278.             comment (zooname, option); break;
  279.          default:
  280.             goto give_list;
  281.       }
  282. zooexit (0);      /* don't fall through */
  283.  
  284. /* usage list including Novice commands */
  285. show_usage:
  286.    fprintf (stderr, "%s\n\n%s%s%s", version, usage, nov_usage, nov_cmds);
  287.     zooexit (1);
  288.  
  289. /* brief usage list */
  290. give_list:
  291.     fprintf (stderr, usage); zooexit (1);
  292.  
  293. /* help screen */
  294. bigusage:
  295. printf("\n\n\n\n\n\n\n\n");
  296. printf ("Zoo archiver, %s\n", version);
  297. printf("(C) Copyright 1991 Rahul Dhesi -- Noncommercial use permitted\n");
  298.  
  299. printf (usage);
  300. printf ("\nChoose a command from within {} and zero or more modifiers from within [].\n");
  301.  
  302. printf ("E.g.:  `zoo a save /bin/*' will archive all files in /bin into save.zoo.\n");
  303. printf ("(Please see the user manual for a complete description of commands.)\n\n");
  304. printf (nov_usage);
  305. printf (nov_cmds);
  306. printf ("\n\n\n\n");
  307. wait_return();    /* print msg & wait for RETURN */
  308.  
  309. printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  310. printf (usage);
  311.  
  312. printf (" Commands in {} mean:         |Modifiers in [] mean:\n");
  313.  
  314. printf ("  a     add files             | a     show archive name(s) in listing\n");
  315. printf ("  c     update comments       | A     apply g or c to archive\n");
  316. printf ("  D     delete stored files   | c     add/list comments\n");
  317. printf ("  e,x   extract files         | d     extract/list deleted files too\n");
  318. printf ("  g     adj. gen. limit/count | dd    extract/list only deleted files\n");
  319. printf ("  l,L,v,V list filenames      | E     erase backup after packing\n");
  320. printf ("  P     pack archive          | f     fast add (no compression) or list\n");
  321. printf ("  T     fix archive datestamp | M     move when adding (erase original)\n");
  322. printf ("  u     add only newer files  | n     add only files not already in archive\n");
  323. printf ("  U     undelete stored files | N     send extracted data to Nowhere\n");
  324. #ifdef FILTER
  325. printf ("  f     act as filter         | c/u   compress/uncompress as filter\n");
  326. #endif /* FILTER */
  327. printf (" -----------------------------  O     don't ask \"Overwrite?\"\n");
  328. printf ("  q     be quiet                p     pipe extracted data to standard output\n");
  329. printf ("  :     don't store dir names   /,//  extract full pathnames\n");
  330. printf ("  .     pack to current dir     I     add filenames read from stdin\n");
  331. printf ("  C     show file CRC value     +/-   enable/disable generations\n");
  332. printf ("  S     overwrite newer files   g     list generation limits\n");
  333. printf ("  P     pack after adding       @n    start extract/list at position n\n");
  334.  
  335. #ifdef FATTR
  336. printf ("  m     list file modes         OO    overwrite read-only files\n");
  337. #endif /* FATTR */
  338. printf ("  C     change archive cmnt     h     use high-compression method\n");
  339. #endif /* end of not OOZ */
  340.  
  341. /* NOTE:  if allowed to fall through and return without an exit() statement,
  342.    it was printing garbage--corrupted stack?  Why--bug in Microsoft C? */
  343. zooexit (1);
  344. return 1;    /* keep lint & compilers happy */
  345. }
  346.  
  347. /* multi-screen help facility thanks to Bill Davidsen */
  348.  
  349. /* help screens */
  350. static char *scrn1[] = {
  351.    "",
  352.     "command line format:",
  353.     "  zoo {command}[options] archive files(s)",
  354.     "",
  355.     "Commands:",
  356.     " a  add files",
  357.     "    u - update, replace only if file is newer than saved version",
  358.     "    n - new, add if file is not in archive",
  359.     "    f - fast, don't compress at all",
  360.     "    h - high performance compressor, slower than default",
  361.     "    M - move files to archive, delete after saving",
  362.     "    c - add a comment to each file added",
  363.     "    C - add a comment to the archive as a whole",
  364.     "    : - strip directory names, save filenames only",
  365.     "    q - quiet (qq suppresses warnings, qqq suppresses nonfatal errors too)",
  366.     "    P - pack after adding, remove overwritten or deleted files",
  367.     "        (leaves a .bak file, use PP to overwrite it)",
  368.     "    I - read filenames from standard input",
  369.     "    + - enable generations",
  370. #ifdef TOS
  371.        "    // - recursively descend directories",
  372. #endif
  373.     (char *)NULL
  374. };
  375.  
  376. static char *scrn2[] = {
  377.    "",
  378.     " e  extract files",
  379.     " x  extract files",
  380.     "    : - extract to current directory (ignore pathnames)",
  381.    "    . - make absolute pathnames relative to current directory",
  382.     "        (name /etc/hosts becomes ./etc/hosts)",
  383.     "    / - extract to subdirs, // create subdirs as needed - default",
  384.     "        (the sequence :/ may be used to use but not create subdirs)",
  385.     "    q - quiet",
  386.     "    d - extract deleted files, too. dd extract *only* deleted files",
  387.     "    N - extract to nowhere. Used to test the archive with xN or xNq",
  388.     "    p - extract for pipe to standard output. Use q to avoid header",
  389.     "",
  390. #ifdef FATTR
  391.     "    O - overwrite without asking, OO overwrites readonly files",
  392. #else
  393.     "    O - overwrite without asking",
  394. #endif
  395.     "    S - overwrite superceded (newer) files",
  396.     (char *)NULL
  397. };
  398.  
  399. static char *scrn3[] = {
  400.    "",
  401.     " l  list archive info",
  402.     " L  list info for multiple archives",
  403.     " v  list verbose file info and archive comment",
  404.     " V  list verbose file info, archive and file comments",
  405.     "    v - verbose (same as v command, used with L for multiple files",
  406.     "    V - verbose with file comments",
  407.     "    C - show CRC",
  408.     "    a - show archive name in file listing (useful with L)",
  409. #ifdef FATTR
  410.     "    m - mode, show file modes in octal",
  411. #endif
  412.     "    d - show deleted files",
  413.     "    q - quiet, shows only file info, no comments or headers",
  414.     "    f - fast, lists only filename, no pathname, multiple columns",
  415.     "    1 - one column output (for the f option)",
  416.     "",
  417.     " c  comment changes, change or add comments to listed files",
  418.     "    (changes all file comments if no files given)",
  419.     "    A - only change archive comment",
  420.     (char *)NULL
  421. };
  422.  
  423. static char *scrn4[] = {
  424.    "",
  425.     " P  pack archive, remove deleted or overwritten files",
  426.     "    E - erase the .bak file when done",
  427.     "",
  428.     " D  delete files by name",
  429.     "    P - pack after deletion, use PP if .bak file exists",
  430.     "    q - quiet",
  431.     "",
  432.     " T  timestamp adjust, make archive age of newest file",
  433.     "",
  434.     " g  generation commands",
  435.     "    l - set generation limit on files",
  436.     "    A - apply limit to archive rather than a file (with gl)",
  437.     "",
  438.     " f  filter, copy stdin to stdout with [de]compression",
  439.     "    c - compress",
  440.     "    u - uncompress",
  441.     "    h - use the high compression method",
  442.     (char *)NULL
  443. };
  444.  
  445. static char *scrn5[] = {
  446.    "",
  447.     "Examples:",
  448.     "",
  449.     "# just add a few files",
  450.     "    zoo a arch file1 files",
  451.     "# add C source files in subdirectories",
  452.     "    zoo a test part1/*.c part2/*.c",
  453.     "# add documentation files with high compression",
  454.     "    zoo ah test *.doc",
  455.     "",
  456.     "# extract all files",
  457.     "    zoo x test",
  458.     "# extract files into the current directory",
  459.     "    zoo x: test",
  460.     "# extract a single file and sort before listing",
  461.     "    zoo xp test users.lst | sort",
  462.     "",
  463.     "# list the contents and archive comments",
  464.     "    zoo v arch",
  465.     "# list all files in all archives",
  466.     "    zoo L xxx.zoo /doc/*.zoo ../*.zoo",
  467.     (char *)NULL
  468. };
  469.  
  470. static char **screens[] = {
  471.     scrn1,        /* intro and add */
  472.     scrn2,        /* extract */
  473.     scrn3,        /* list commands */
  474.     scrn4,        /* other commands */
  475.     scrn5,        /* add and extract examples */
  476.     (char **)NULL
  477. };
  478.  
  479. /* multi-screen help routine */
  480. static void ms_help(options)
  481. char *options;
  482. {
  483. #ifndef SZ_SCREEN            /* screen size can be overridden in options.h */
  484. # define SZ_SCREEN 24
  485. #endif
  486.     int scrnlen = SZ_SCREEN;
  487.     char ***curscreen, **curline;
  488.     int linecount;
  489.  
  490.     /* if "Hnn" output in nn line format */
  491.     if (++options) sscanf(options, "%d", &scrnlen);
  492.     if (scrnlen < 2)
  493.         scrnlen = SZ_SCREEN;
  494.  
  495.     /* loop thru screens */
  496.     for (curscreen = screens; *curscreen != NULL; ++curscreen) {
  497.         printf("\n\n\n\n");
  498.         linecount = scrnlen;
  499.         curline = *curscreen;
  500.         while (*curline != NULL) {
  501.             printf("%s\n", *(curline++));
  502.             --linecount;
  503.         }
  504.  
  505.         /* slew page */
  506.         while (--linecount != 0) putchar('\n');
  507.         wait_return();    /* print msg & wait for RETURN */
  508.     }
  509.  
  510.     exit(0);
  511. }
  512.  
  513. /* wait_return prints a message, then waits until user hits RETURN key,
  514. then returns.  Special cases:  (a) if not interactive (as tested with
  515. isatty() if available), it returns immediately; (b) while waiting for
  516. RETURN, if EOF occurs, it causes zooexit(0) */
  517.  
  518. static void wait_return() 
  519. {
  520. #ifdef HAVE_ISATTY
  521.     if (!isatty(fileno(stdout)) || !isatty(fileno(stdin)))
  522.         return;
  523. #endif
  524.     (void) printf("Hit RETURN (or ENTER) key to continue...");
  525.     for ( ; ;) {
  526.         int key; 
  527.         key = getchar();
  528.         if (key == EOF)
  529.             zooexit(0);
  530.         if (key == '\n' || key == '\r')
  531.             return;
  532.     }
  533. }
  534.